import Vue from "vue";
import VueRouter from "vue-router";
// 路由懒加载
const Login = () =>
  import(
    /* webPackChunkName : "login_home_welcome" */ "../components/login.vue"
  );
const Home = () =>
  import(
    /* webPackChunkName : "login_home_welcome" */ "../components/home.vue"
  );
const Welcome = () =>
  import(
    /* webPackChunkName : "login_home_welcome" */ "../components/welcome.vue"
  );
const Users = () =>
  import(
    /* webPackChunkName : "users_rights_roles" */ "../components/user/users.vue"
  );
const Rights = () =>
  import(
    /* webPackChunkName : "users_rights_roles" */ "../components/power/rights.vue"
  );
const Roles = () =>
  import(
    /* webPackChunkName : "users_rights_roles" */ "../components/power/roles.vue"
  );

const Categories = () =>
  import(
    /* webPackChunkName : "Categories_Params" */ "../components/goods/categories.vue"
  );

const Params = () =>
  import(
    /* webPackChunkName : "Categories_Params" */ "../components/goods/params.vue"
  );
const Goods = () =>
  import(
    /* webPackChunkName : "goods_addGoods" */ "../components/goods/goods.vue"
  );
const AddGoods = () =>
  import(
    /* webPackChunkName : "goods_addGoods" */ "../components/goods/goods.vue"
  );
const Orders = () =>
  import(
    /* webPackChunkName : "order_reports" */ "../components/order/orders.vue"
  );
const Reports = () =>
  import(
    /* webPackChunkName : "order_reports" */ "../components/reports/reports.vue"
  );

// import Login from "../components/login.vue";
// import Home from "../components/home.vue";
// import Welcome from "../components/welcome.vue";
// import Users from "../components/user/users.vue";
// import Rights from "../components/power/rights.vue";
// import Roles from "../components/power/roles.vue";

// import Categories from "../components/goods/categories.vue";
// import Params from "../components/goods/params.vue";
// import Goods from "../components/goods/goods.vue";
// import AddGoods from "../components/goods/addGoods.vue";
// import Orders from "../components/order/orders.vue";
// import Reports from "../components/reports/reports.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/login"
  },
  {
    path: "/login",
    component: Login
  },
  {
    path: "/home",
    component: Home,
    children: [
      {
        path: "",
        redirect: "welcome"
      },
      {
        path: "welcome",
        component: Welcome
      },
      //  子路由的完整路由并不一定需要在该父路由后面
      {
        path: "/users",
        component: Users
      },
      {
        path: "/rights",
        component: Rights
      },
      {
        path: "/roles",
        component: Roles
      },
      {
        path: "/categories",
        component: Categories
      },
      {
        path: "/params",
        component: Params
      },
      {
        path: "/goods",
        component: Goods
      },
      {
        path: "/goods/addGoods",
        component: AddGoods
      },
      {
        path: "/orders",
        component: Orders
      },
      {
        path: "/reports",
        component: Reports
      }
    ]
  }
];

const router = new VueRouter({
  routes,
  mode: "history" // 这里设置history模式 在上线时会报错
});

// 使用全局导航守卫，在跳转路由之前判断window.sessionStorage中是否存在token再进行跳转 控制路由访问权限
router.beforeEach((to, from, next) => {
  if (to.path === "/login") return next(); // 如果访问的是登录页则直接放行
  const tokenStr = window.sessionStorage.getItem("token");
  if (!tokenStr) return next("/login"); // 如果没有token值，则跳转到登录页
  next();
});

export default router;
